package com.eimf.saf.security.filter;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.eimf.saf.base.util.PropsLoader;
import com.eimf.saf.base.util.SpringContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * @author eden
 * @date 2013-11-13 下午3:05:32
 * @Description: 将特殊字符替换成制定字符
 */
public class SpecCharInterceptor implements Interceptor
{

    private static final long serialVersionUID = 1L;
    private static Log logger = LogFactory.getLog(SpecCharInterceptor.class);

    /** 配置的特殊字符 */
    private String[] specChars = null;

    /** 配置特殊字符的替代字符 */
    private String[] newSpecChars = null;

    /** 是否需要拦截 */
    private boolean isHandle = false;

    @Override
    public void destroy()
    {
    }

    @Override
    public void init()
    {
        final PropsLoader props = (PropsLoader) SpringContext.getBean("propsLoader");
        final String specChar = props.props.getProperty("spec.chars");
        final String newSpecChar = props.props.getProperty("spec.new.chars");

        if (specChar != null && newSpecChar != null)
        {
            specChars = specChar.split(",");
            newSpecChars = newSpecChar.split(",");
            if (specChar != null && specChar.trim().length() > 0 && specChars.length == newSpecChars.length)
            {
                isHandle = true;
            }
        }

        if (isHandle)
        {
            SpecCharInterceptor.logger.info("特殊字符拦截处理器启动成功。");
            SpecCharInterceptor.logger.info("这些特殊字符：" + specChar + "将替换成：" + newSpecChar);
        } else
        {
            SpecCharInterceptor.logger.info("特殊字符拦截处理器启动失败。原因可能是：特殊字符没有配置或特殊字符和新字符数量不一致。");
        }
    }

    @Override
    public String intercept(final ActionInvocation arg0) throws Exception
    {
        doSpecChar(arg0.getInvocationContext());
        final String result = arg0.invoke();
        return result;
    }

    /**
     * 处理特殊字符，如果是文本域中含有配置文件中配置的字符，将把这些字符改为配置的其它字符。
     * 
     * @param servletRequest
     */
    @SuppressWarnings("unchecked")
    private void doSpecChar(final ActionContext actionContext)
    {
        if (isHandle)
        {
            for (final Object item : actionContext.getParameters().entrySet())
            {
                final Map.Entry<String, String[]> temp = (Map.Entry<String, String[]>) item;
                if (temp.getValue() != null)
                {
                    final String[] newValue = new String[temp.getValue().length];
                    for (int i = 0; i < newValue.length; i++)
                    {
                        String s = temp.getValue()[i];
                        for (int j = 0; j < specChars.length; j++)
                        {
                            s = s.replaceAll(specChars[j], newSpecChars[j]);
                        }
                        newValue[i] = s;
                    }
                    temp.setValue(newValue);
                }
            }
        }
    }
}
